package com.hc.demo.exception;

import com.hc.demo.dto.ResultResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    //处理APP的业务异常

    @ResponseBody
    @ExceptionHandler(TijianException.class)
    public ResultResponse handlerTijianException(TijianException e){
        ResultResponse resultResponse=null;
        System.out.println("e:"+e.getMessage());
        e.printStackTrace();
        resultResponse=new ResultResponse(e.getExCode(),e.getMessage(),null);
        return resultResponse;
    }
    //兜底的方法
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResultResponse handlerAllException(Exception e){
        ResultResponse resultResponse=null;
        System.out.println("e:"+e.getMessage());
        e.printStackTrace();
        resultResponse=new ResultResponse(TijianExceptionEnum.SYSTEM_ERROR.getEnCode(),
                TijianExceptionEnum.SYSTEM_ERROR.getEnMessage());

        return resultResponse;
    }
}
